﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZX.HIS.BLL;
using ZX.HIS.Common;

namespace ZX.HIS.UI.Controllers
{
    public class DrugWareHouseController : Controller
    {
        //
        // GET: /DrugWareHouse/
        ZX.HIS.BLL.DrugWareHouse bll = new ZX.HIS.BLL.DrugWareHouse();
        ZX.HIS.BLL.DrupInfo tbll = new ZX.HIS.BLL.DrupInfo();
        /// <summary>
        /// 药品入库页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            List<ZX.HIS.Model.DrugWareHouse> wlist = bll.DrugWareHouseList();
            List<ZX.HIS.Model.DrupInfo> dlist = tbll.DrupInfoList();
            ViewData["DrupInfo"] = dlist;
            return View(wlist);
        }
        /// <summary>
        /// 读取药品信息表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DrugShow(int id)
        {
            ZX.HIS.Model.DrupInfo tt = new Model.DrupInfo();

            List<ZX.HIS.Model.DrupInfo> tlist = tbll.DrupInfoList();
            tt = tlist.Where(p => p.DrugId == id).FirstOrDefault();
            ViewData["tt"] = tt;

            ViewData["DrupInfo"] = tlist;
            return View("Index");
        }
        /// <summary>
        /// 添加药品（入库）
        /// </summary>
        /// <returns></returns>
        public ActionResult AddDrugWareHouse()
        {
            ZX.HIS.Model.DrugWareHouse dmodel = new Model.DrugWareHouse();
            dmodel.HospitalId = 0;
            HttpCookie myCookie = Request.Cookies["MyCookie"];
            if (myCookie != null)
            {
                if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                {
                    dmodel.HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                }
            }
            dmodel.DrugWareName = Request["DrugName"];
            dmodel.DrugWarePackSpece = Request["DrugPackSpece"];
            dmodel.ProductId = int.Parse(Request["ProductId"]);
            dmodel.DrugWareNumber = int.Parse(Request["DrugWareNumber"]);
            dmodel.DrugPurchasePrice = decimal.Parse(Request["DrugPurchasePrice"]);
            dmodel.DrugWarePresPrice = decimal.Parse(Request["DrugPresPrice"]);
            dmodel.DrugWareBatchNo = int.Parse(Request["DrugWareBatchNo"]);
            dmodel.DrugWareTime = DateTime.Parse(Request["DrugWareTime"]);
            dmodel.UserId = 1001;
            dmodel.DrugNowDate = DateTime.Parse(DateTime.Now.ToString("yyyy/MM/dd"));
            if (bll.DrugWareHouseAdd(dmodel) > 0)
            {
                return Content("<script>alert('保存成功');location.href='/DrugWareHouse/DrugWareHouseshow'</script>");
            }
            else
            {
                return Content("<script>alert('保存失败')</script>");
            }
        }
        /// <summary>
        /// 药品管理
        /// </summary>
        /// <returns></returns>
        public ActionResult DrugWareHouseshow()
        {
            return View();
        }
        /// <summary>
        /// 查询厂家信息
        /// </summary>
        /// <returns></returns>
        public JsonResult Productlist()
        {
            ZX.HIS.BLL.Product pbll = new ZX.HIS.BLL.Product();
            List<ZX.HIS.Model.Product> Plist = pbll.ProductList();
            return Json(Plist);
        }
        /// <summary>
        /// 分页显示库存管理
        /// </summary>
        /// <returns></returns>
        public ActionResult show()
        {
            int ProductId = 0;
            if (Request["ProductId"] != null && Request["ProductId"] != "0")
            {
                ProductId = Convert.ToInt32(Request["ProductId"]);
            }
            string PostDate = "1900-01-01";
            if (Request["PostDate"] != null && Request["PostDate"] != "")
            {
                PostDate = Request["PostDate"];
            }
            string PostDate1 = "1900-01-01";
            if (Request["PostDate1"] != null && Request["PostDate1"] != "")
            {
                PostDate1 = Request["PostDate1"];
            }
            int PageIndex = 1;
            if (Request["PageIndex"] != null && Request["PageIndex"] != "0")
            {
                PageIndex = Convert.ToInt32(Request["PageIndex"]);
            }
            int PageSize = 5;
            PageClass<ZX.HIS.Model.DrugWareHouse> PageList = bll.DrugWareHouseshow(ProductId, PostDate, PostDate1, PageIndex, PageSize);
            return Json(PageList);
        }
        /// <summary>
        /// 编辑库存管理页面
        /// </summary>
        /// <param name="id"></param>
        /// <param name="tt"></param>
        /// <returns></returns>
        public ActionResult UptDrugWareHouse(string id, ZX.HIS.Model.DrugWareHouse tt)
        {
            List<ZX.HIS.Model.DrugWareHouse> tlist = bll.DrugWareHouseList();
            tt = tlist.Where(p => p.DrugWareId == int.Parse(id)).FirstOrDefault();
            //ZX.HIS.BLL.Product pbll = new ZX.HIS.BLL.Product();
            //List<ZX.HIS.Model.Product> Plist = pbll.ProductList();


            //List<SelectListItem> select1 = new List<SelectListItem>();
            //foreach (var item in Plist)
            //{
            //    SelectListItem smodel = new SelectListItem();
            //    smodel.Text = item.ProductName;
            //    smodel.Value = item.ProductId.ToString();
            //    if (item.ProductId == tt.ProductId)
            //    {
            //        smodel.Selected = true;
            //    }
            //    select1.Add(smodel);
            //}
            //ViewData["select1"] = select1;
            return View(tt);
        }
        /// <summary>
        /// 判断是否编辑成功
        /// </summary>
        /// <param name="ll"></param>
        /// <returns></returns>
        public int DrugWareHouseUptdate(ZX.HIS.Model.DrugWareHouse ll)
        {
            if (bll.DrugWareHouseUpt(ll) != null)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public ActionResult DrugWareHouseNum()
        {
            return View();
        }
        public ActionResult Pageshow()
        {
            ZX.HIS.Model.DrugWareHouse DModel = new Model.DrugWareHouse();
            DModel.ProductId = 0;
            DModel.HospitalId = 0;
            HttpCookie myCookie = Request.Cookies["MyCookie"];
            if (myCookie != null)
            {
                if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                {
                    DModel.HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                }
            }
            if (Request["ProductId"] != null && Request["ProductId"] != "0")
            {
                DModel.ProductId = Convert.ToInt32(Request["ProductId"]);
            }

            int PageIndex = 1;
            if (Request["PageIndex"] != null && Request["PageIndex"] != "0")
            {
                PageIndex = Convert.ToInt32(Request["PageIndex"]);
            }
            int PageSize = 5;
            PageClass<ZX.HIS.Model.DrugWareHouse> PageList = bll.DrugWarePageList(PageIndex, PageSize, DModel);
            return Json(PageList);
        }

        public ActionResult DrugWareHouseValidity()
        {
            ZX.HIS.BLL.DrugTypeInfo tbll = new BLL.DrugTypeInfo();
            List<ZX.HIS.Model.DrugTypeInfo> liat = tbll.DrugTypeShow();
            ViewData["liat"] = liat;
            return View();
        }
        public JsonResult pageDrugWareHouseshow()
        {
            ZX.HIS.Model.DrugWareHouse DModel = new Model.DrugWareHouse();

            if (!String.IsNullOrEmpty(Request["DrugTypeId"]))
            {
                DModel.DrugTypeId = Request["DrugTypeId"].ToString();
            }
            int date = Convert.ToInt32(Request["drugDate"]);
            DateTime dt1 = DateTime.Now.AddDays(date);
            DModel.DrugWareTime = dt1;
            int PageIndex = 1;
            if (Request["PageIndex"] != null && Request["PageIndex"] != "0")
            {
                PageIndex = Convert.ToInt32(Request["PageIndex"]);
            }
            int PageSize = 3;
            if (Request["PageSize"] != null && Request["PageSize"] != "0")
            {
                PageSize = Convert.ToInt32(Request["PageSize"]);
            }
            PageClass<ZX.HIS.Model.DrugWareHouse> PageDrugshow = bll.DrugWareHousePageList(PageIndex, PageSize, DModel);
            return Json(PageDrugshow);
        }

    }
}
